<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="归属房产" prop="houseId">
        <el-tree-select
          v-model="formData.houseId"
          :data="houseList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          placeholder="请选择归属房产"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipTypeId">
        <el-tree-select
          v-model="formData.equipTypeId"
          :data="equipTypeList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          placeholder="请选择设备类型"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="devNo">
        <el-input v-model="formData.devNo" placeholder="请输入设备编号" />
      </el-form-item>
      <el-form-item label="设备编码" prop="devCode">
        <el-input v-model="formData.devCode" placeholder="请输入设备编码" />
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="业务域" prop="busScope">
        <el-select v-model="formData.busScope" placeholder="请选择业务域" class="w-1/1">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.IOTMANAGE_EQUIP_BUS_SCOPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属产品" prop="refProdCode">
        <el-input v-model="formData.refProdCode" placeholder="请输入所属产品" />
      </el-form-item>
      <el-form-item label="绑定网关设备" prop="wgDevCode">
        <el-input v-model="formData.wgDevCode" placeholder="请输入绑定网关设备" />
      </el-form-item>
      <el-form-item label="最小值" prop="minRange">
        <el-input v-model="formData.minRange" placeholder="请输入最小值" />
      </el-form-item>
      <el-form-item label="最大值" prop="maxRange">
        <el-input v-model="formData.maxRange" placeholder="请输入最大值" />
      </el-form-item>
      <el-form-item label="状态" prop="status1">
        <el-select v-model="formData.status1" placeholder="请输入状态" class="w-1/1">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.IOTMANAGE_EQUIP_STATUS1)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="附件" prop="vdef2">
        <!-- TODO -->
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EquipHouseApi, EquipHouseVO } from '@/api/iotmanage/equiphouse'
import { defaultProps, handleTree } from '@/utils/tree'
import * as HouseApi from '@/api/house/category'
import * as EquipCategoryApi from '@/api/iotmanage/equipcategory'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
/** 房产设备关系 表单 */
defineOptions({ name: 'EquipHouseForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const houseList = ref<Tree[]>([]) // 房产设备关系
const equipTypeList = ref<Tree[]>([]) // 房产设备关系
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改

const formData = ref({
  id: undefined,
  houseId: undefined,
  equipTypeId: undefined,
  devNo: undefined,
  devCode: undefined,
  name: undefined,
  busScope: undefined,
  refProdCode: undefined,
  wgDevCode: undefined,
  minRange: undefined,
  maxRange: undefined,
  status1: undefined
})
const formRules = reactive({
  houseId: [{ required: true, message: '归属房产不能为空', trigger: 'blur' }],
  equipTypeId: [{ required: true, message: '设备类型不能为空', trigger: 'blur' }],
  devNo: [{ required: true, message: '设备编号不能为空', trigger: 'blur' }],
  devCode: [{ required: true, message: '设备编码不能为空', trigger: 'blur' }],
  devName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EquipHouseApi.getEquipHouse(id)
    } finally {
      formLoading.value = false
    }
  }
  houseList.value = handleTree(await HouseApi.CategoryApi.getCategoryList(null))
  equipTypeList.value = handleTree(
    await EquipCategoryApi.EquipCategoryApi.getEquipCategoryList(null)
  )
  // await EquipHouseApi.
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EquipHouseVO
    if (formType.value === 'create') {
      await EquipHouseApi.createEquipHouse(data)
      message.success(t('common.createSuccess'))
    } else {
      await EquipHouseApi.updateEquipHouse(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    houseId: undefined,
    equipTypeId: undefined,
    devNo: undefined,
    devCode: undefined,
    name: undefined,
    busScope: undefined,
    refProdCode: undefined,
    wgDevCode: undefined,
    minRange: undefined,
    maxRange: undefined,
    status1: undefined
  }
  formRef.value?.resetFields()
}
</script>
